class Pipe {
    constructor(game) {
        this.game = game
        this.name = 'pipes'
        this.setUp()
    }

    static new = function(game) {
        return new this(game)
    }

    debug() {
        if (this.horizontalInterval != config.horizontalInterval.value) {
            this.horizontalInterval = config.horizontalInterval.value
            this.columOfPipes= Math.floor(500 / this.horizontalInterval)
            this.pipes = []
        for (var i = 0; i < this.columOfPipes;i++ ) {
            var p1 = new GuaImage(this.game, 'pipe')
            p1.flipY = true
            p1.setPosition(500 + i * this.horizontalInterval)
            var p2 = new GuaImage(this.game, 'pipe')
            p2.setPosition(p1.x)
            this.resetPipePosition(p1, p2)
            this.pipes.push(p1)
            this.pipes.push(p2)
        }
    }
    if (this.verticalInterval != config.verticalInterval.value) { 

        this.verticalInterval = config.verticalInterval.value
        for (var i = 0; i < this.columOfPipes;i++ ) {
            var p1 = this.pipes[i] 
            var p2 = this.pipes[i+1]
            p2.setPosition(p2.x , p1.y + p1.h + this.verticalInterval)
    }
    }
}

    setUp() {
        this.flipX = false
        this.rotation = 0
        this.pipes = []
        this.verticalInterval = config.verticalInterval.value
        this.horizontalInterval = config.horizontalInterval.value
        this.columOfPipes= Math.floor(500 / this.horizontalInterval)
        for (var i = 0; i < this.columOfPipes;i++ ) {
            var p1 = new GuaImage(this.game, 'pipe')
            p1.flipY = true
            p1.setPosition(500 + i * this.horizontalInterval)
            var p2 = new GuaImage(this.game, 'pipe')
            p2.setPosition(p1.x)
            this.resetPipePosition(p1, p2)
            this.pipes.push(p1)
            this.pipes.push(p2)
        }
    }

    resetPipePosition(p1, p2) {
        p1.setPosition(p1.x , randomBetween(-270, -150))
        p2.setPosition(p2.x , p1.y + p1.h + this.verticalInterval)
    }


    update() {
        for (let i = 0; i < this.pipes.length; i+=2) {
            let p1 = this.pipes[i]
            let p2 = this.pipes[i + 1]
            p1.move(-5, 0)
            p2.move(-5, 0)
            if (p1.x < -100) {
                p1.move(this.horizontalInterval * this.columOfPipes, 0)
                p2.move(this.horizontalInterval * this.columOfPipes, 0)
                this.resetPipePosition(p1, p2)
                }
            }
        }
    draw() {
        for (var p of this.pipes) { 
            this.texture = p
            this.game.context.save()

            var centerX = this.texture.x + this.texture.w / 2
            var centerY = this.texture.y + this.texture.h / 2
            this.game.context.translate(centerX, centerY)

            var scaleX = p.flipX ? -1 : 1
            var scaleY = p.flipY ? -1 : 1
            this.game.context.scale(scaleX, scaleY)
            this.game.context.rotate(this.rotation * Math.PI / 180)
            this.game.context.translate(-this.texture.w / 2, - this.texture.h / 2)

            this.game.context.drawImage(this.texture.texture, 0, 0)

            this.game.context.restore()
        }

    }

}